package com.baseproject.datasource.remote;

import com.baseproject.rx.OnErrorHandler.BaseErrorHandler;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by asus on 2017-09-13.
 */

public class RetryWhenProcess implements Func1<Observable<? extends Throwable>, Observable<?>> {

    private long mInterval;

    int retryTimes = 0;
    public static int MAX_RETRY_TIMES = 5;

    public RetryWhenProcess() {
    }

    @Override
    public Observable<?> call(Observable<? extends Throwable> errors) {
        return errors.flatMap(new Func1<Throwable, Observable<?>>() {
            @Override
            public Observable<?> call(Throwable throwable) {
                return BaseErrorHandler.createErrorHandler(throwable).retry(retryTimes++);
            }
        });

//        //官网方案，可控但最后回调的是onComplete
//        return observable.zipWith(Observable.range(1, 5), new Func2<Throwable, Integer, Integer>() {
//            @Override
//            public Integer call(Throwable throwable, Integer i) {
//                return i;
//            }
//        })
//                .flatMap(new Func1<Integer, Observable<?>>() {
//                    @Override
//                    public Observable<?> call(Integer retryCount) {
//                            LogUtils.d("Repository", "delay retry by " + retryCount + " second(s)");
//                            return Observable.timer(retryCount, TimeUnit.SECONDS);
//
//                    }
//                });
    }

}
